<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="历史版本" prop="hisVersion">
        <el-select  @change="changeHisVersion($event)" v-model="queryParams.hisVersion" placeholder="请选择历史版本号" clearable>
          <el-option
            v-for="dict in hisVersions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            @keyup.enter.native="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="需求/BUG编号" prop="demandBugNum">
        <el-input
          v-model="queryParams.demandBugNum"
          placeholder="请输入需求/BUG编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="需求/BUG内容" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入需求BUG内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="需求/BUG" prop="demandBugType">
        <el-select filterable v-model="queryParams.demandBugType" placeholder="请选择版本号" clearable>
          <el-option
            v-for="demandBugType in demandBugTypes"
            :key="demandBugType.value"
            :label="demandBugType.label"
            :value="parseInt(demandBugType.value)"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="外部/内部" prop="demandBugFsource">
        <el-select filterable v-model="queryParams.demandBugFsource" placeholder="请选择版本号" clearable>
          <el-option
            v-for="demandBugFsource in demandBugFsources"
            :key="demandBugFsource.value"
            :label="demandBugFsource.label"
            :value="parseInt(demandBugFsource.value)"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="版本号" prop="version">
        <el-select filterable v-model="queryParams.version" placeholder="请选择版本号" clearable>
          <el-option
            v-for="dict in dict.type.report_version"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="需求/BUG日期">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @keyup.enter.native="handleQuery"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reportworld:demandbuginfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['reportworld:demandbuginfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reportworld:demandbuginfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reportworld:demandbuginfo:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleImport"
          v-hasPermi="['reportworld:demandbuginfo:import']"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border="border" max-height="550px" v-loading="loading" :data="infoList" @selection-change="handleSelectionChange"
              @sort-change="handleSortChange" :row-style="tableRowStyle" @row-click="rowClick"  ref="demandbuginfo">
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-hasPermi="['reportworld:demandbuginfo:query']"
          label="操作" align="center" width="100" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleUpdate(scope.row,false)"
                v-hasPermi="['reportworld:demandbuginfo:query']"
              >详情</el-button>
            </template>
        </el-table-column>
      <el-table-column label="需求/BUG编号" width="180" align="center" prop="demandBugNum" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="需求/BUG" width="150" align="center"
                       :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">{{ scope.row.demandBugType === 1 ? '需求' : 'BUG' }}</template>
      </el-table-column>
      <el-table-column label="外部/内部" width="150" align="center"
                       :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">{{ scope.row.demandBugFsource === 1 ? '外部' : '内部' }}</template>
      </el-table-column>
      <el-table-column label="版本号" width="180" align="center" prop="version" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="需求/BUG内容" width="200" align="center" prop="description" :show-overflow-tooltip="true"/>
      <el-table-column label="需求/BUG日期" align="center" prop="demandBugDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.demandBugDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="180" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加人" align="center" width="150" prop="createBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="编辑时间" align="center" prop="updateTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编辑人" align="center" width="150" prop="updateBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改需求bug信息对话框 -->
    <el-dialog  :title="title" :close-on-click-modal ="false" :visible.sync="open" width="800px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="110px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="需求/BUG编号" prop="demandBugNum">
                <el-input clearable v-model="form.demandBugNum" maxlength="30" placeholder="请输入需求/者bug编号"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="需求/BUG" prop="demandBugType">
                <el-radio-group v-model="form.demandBugType">
                  <el-radio
                    v-for="demandBugType in demandBugTypes"
                    :key="demandBugType.value"
                    :label="parseInt(demandBugType.value)"
                  >{{demandBugType.label}}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="外部/内部" prop="demandBugFsource">
                <el-radio-group v-model="form.demandBugFsource">
                  <el-radio
                    v-for="demandBugFsource in demandBugFsources"
                    :key="demandBugFsource.value"
                    :label="parseInt(demandBugFsource.value)"
                  >{{demandBugFsource.label}}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="需求BUG日期" prop="demandBugDate">
                <el-date-picker clearable
                                v-model="form.demandBugDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择需求BUG日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="需求/BUG内容" prop="description">
                <el-input  clearable v-model="form.description" type="textarea" placeholder="请输入内容"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="版本号" prop="version">
                <el-select :disabled="true" clearable v-model="form.version" placeholder="请选择版本号">
                  <el-option
                    v-for="dict in dict.type.report_version"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status">
                  <el-radio
                    v-for="dict in dict.type.sys_common_status"
                    :key="dict.value"
                    :label="parseInt(dict.value)"
                  >{{dict.label}}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input maxlength="500" clearable v-model="form.remark" type="textarea" placeholder="请输入内容"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="isUpdate" :loading="butloading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog  :title="upload.title" :close-on-click-modal ="false" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport+'&hisVersion='+ this.queryParams.hisVersion"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo ,init} from "@/api/reportworld/demandbuginfo";
import { getToken } from "@/utils/auth";
import { getTableRowStyle } from "@/utils/yss";
export default {
  name: "demandbuginfo",
  dicts: ['sys_common_status', 'sys_yes_no', 'report_version'],
  data() {
    return {
      butloading:false,
      isUpdate:false,
      // 数据范围选项
      demandBugTypes: [
        {
          value: 1,
          label: "需求"
        },
        {
          value: 2,
          label: "BUG"
        }
      ],
      demandBugFsources: [
        {
          value: 1,
          label: "外部"
        },
        {
          value: 2,
          label: "内部"
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 需求bug信息表格数据
      infoList: [],
      // 日期范围
      dateRange: [],
      // 历史版本号
      hisVersions:[],
      hisVersion:'',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/reportworld/demandBugInfo/readDemandBugInfo"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        demandBugNum: null,
        demandBugType: null,
        demandBugFsource: null,
        version: null,
        status: null,
        demandBugDate: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
        hisVersion:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        demandBugNum: [
          { required: true, message: "需求/者bug编号不能为空", trigger: "blur" }
        ],
        demandBugType: [
          { required: true, message: "需求/者bug不能为空", trigger: "change" }
        ],
        demandBugFsource: [
          { required: true, message: "外部/内部不能为空", trigger: "change" }
        ],
        version: [
          { required: true, message: "版本号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        demandBugDate: [
          { required: true, message: "需求BUG日期不能为空", trigger: "blur" }
        ] ,
        description: [
          { required: true, message: "需求内容不能为空", trigger: "blur" }
        ]
        ,
      }
    };
  },
  created() {
    this.init();
    this.getList();
  },
  methods: {
    changeHisVersion(value){
      this.queryParams.hisVersion=value;
      this.init();
      this.getList();
    },
    init(){
      init(this.queryParams).then(response => {
         this.hisVersions=response.hisVersions;
    });
    },
    /** 查询需求bug信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        demandBugNum: null,
        demandBugType: null,
        demandBugFsource: null,
        version: null,
        status: null,
        demandBugDate: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
        hisVersion:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
      /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
     },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加需求BUG信息";
      this.isUpdate =true;
      this.form.version = this.dict.type.report_version[0].value;
      if(this.queryParams.hisVersion){
        this.form.version =this.queryParams.hisVersion;
      }
      this.form.status = parseInt(this.dict.type.sys_common_status[0].value);
      this.form.demandBugType = parseInt(this.demandBugTypes[0].value);
      this.form.demandBugFsource = parseInt(this.demandBugFsources[0].value);
      var date = new Date();
      this.form.demandBugDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + (date.getDate());
    },
    /** 修改按钮操作 */
    handleUpdate(row,isUpdate) {
      this.reset();
      const id = row.id || this.ids
      this.queryParams.id=id[0];
      if(isUpdate == false){
        this.queryParams.id=id;
      }else{
        this.queryParams.id=id[0];
      }
      getInfo(this.queryParams).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改需求BUG信息";
        this.isUpdate =true;
        if(isUpdate == false){
          this.title = "需求BUG详情";
          this.isUpdate =false;
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.butloading=true;
          this.form.hisVersion =this.queryParams.hisVersion;
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {this.butloading=false});
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {this.butloading=false});
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const hisVersion= this.queryParams.hisVersion;
      this.$modal.confirm('是否确认删除数据项？').then(function() {
        return delInfo(ids,hisVersion);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "需求BUG导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('reportworld/demandBugInfo/importTemplate', {
      }, `需求BUG模板.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
  // 提交上传文件
  submitFileForm() {
    this.$refs.upload.submit();
  },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.ids=JSON.stringify(this.ids);
      this.download('reportworld/demandBugInfo/export', {
        ...this.queryParams
      }, `需求BUG_${new Date().getTime()}.xlsx`)
    },
    tableRowStyle({ row, rowIndex}){
        return getTableRowStyle(row,rowIndex,this.ids);
    },
    rowClick(row, column, event) { //单击行
      this.$refs.demandbuginfo.toggleRowSelection(row);
    }
  }
};
</script>
